/**
 * Copyright(c) Hainan eKing Technology co., LTD
 *
 * @date 2013年8月14日
 * 	
 * Original Author: 郑兴(zhengxing)
 *
 * ChangeLog
 *
 */
package com.hnas.sys.service.system;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hnas.core.dao.PageBean;
import com.hnas.core.dto.DTOPagedList;
import com.hnas.core.util.StringUtil;
import com.hnas.sys.dao.MenuPackageDAO;
import com.hnas.sys.domain.PackageStatus;
import com.hnas.sys.orm.MenuPackage;
import com.hnas.sys.service.system.criteria.DTOMenuSearch;
import com.hnas.sys.service.system.dto.DTOMenuPackage;
import com.hnas.sys.service.system.mapper.MenuPackageMapper;

/**
 * <pre>菜单服务接口实现类</pre>
 * @author 郑兴(zhengxing)
 *
 */
@Component("menuPackageService")
@Transactional
public class MenuPackageServiceImpl implements MenuPackageService {
	@Autowired
	private MenuPackageDAO menuPackageDAO;
	
	private MenuPackage getMenuPackage(DTOMenuPackage dto){
		if (dto.getId()>0)
			return  this.menuPackageDAO.find(dto.getId());
//		if (StringUtil.isNotEmpty(dto.getMenuPackageName())){
//			return this.menuPackageDAO.getMenuPackageByCodeOrName(dto.getMenuPackageName());
//		}
		return null;
	}
	/*
	 * (non-Javadoc)
	 * @see com.hna.dt.service.system.MenuPackageService#saveOrUpdate(com.hna.dt.service.system.dto.DTOMenuPackage)
	 */
	public void saveOrUpdate(DTOMenuPackage dto) {
//		if(0L == dto.getId()) {
//			menuPackageDAO.persist(MenuPackageMapper.getInstance().mapToEntity(dto));
//		} else {
//			menuPackageDAO.merge(MenuPackageMapper.getInstance().mapToEntity(dto));
//		}
		MenuPackage vo = this.getMenuPackage(dto);
		if (null==vo){
			vo = new MenuPackage();
			vo.setStatus(PackageStatus.SHOW);
		}else{
			vo.setStatus(PackageStatus.SHOW);
		}
		BeanUtils.copyProperties(dto, vo, new String[]{"id","status","statusStr"});

		if (vo.getId()==0){
			this.menuPackageDAO.persist(vo);
		}else{
			this.menuPackageDAO.merge(vo);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.hna.dt.service.system.MenuPackageService#find(java.lang.Long)
	 */
	@Transactional(readOnly=true, propagation=Propagation.REQUIRED)
	public DTOMenuPackage find(Long id) {
		return MenuPackageMapper.getInstance().mapToValue(menuPackageDAO.find(id));
	}

	/*
	 * (non-Javadoc)
	 * @see com.hna.dt.service.system.MenuPackageService#loadAll()
	 */
	@Transactional(readOnly=true, propagation=Propagation.REQUIRED)
	public List<DTOMenuPackage> loadAll() {
		return MenuPackageMapper.getInstance().mapToValues(menuPackageDAO.loadAll());
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.hna.dt.service.system.MenuPackageService#search(java.lang.String)
	 */
	@Transactional(readOnly=true, propagation=Propagation.REQUIRED)
	public List<DTOMenuPackage> search(String packageName) {
		if(StringUtil.isEmpty(packageName)) {
			return this.loadAll();
		} else {
			return MenuPackageMapper.getInstance().mapToValues(menuPackageDAO.search(packageName));
		}
	}

	public DTOPagedList<DTOMenuPackage> search(DTOMenuSearch con) {
		// TODO Auto-generated method stub
//		return null;
			PageBean<MenuPackage> page = new PageBean<MenuPackage>();
		page.setPageNo(con.getPageNo());
		page.setPageSize(con.getPageSize());
		page.setTotalCount(con.getTotalCount());
		
		StringBuffer sb = new StringBuffer();
		List<String> paras = new ArrayList<String>();
		int j=1;
		sb.append(" and t.statusStr='SHOW'");
		if (null!=con.getDto()){
		if (StringUtil.isNotEmpty(con.getDto().getName())) {
			sb.append(" and t.name like ?" + j++);
			paras.add("%"+con.getDto().getName().trim()+"%");
		   }
		}
		
 
		
		//sb.append(" and t.statusStr ='SHOW'");
		 
		
		if (sb.length() > 0) {
			sb.delete(0, 5);
			sb.insert(0, " from MenuPackage t where ");
		} else {
			sb.append(" from MenuPackage t");
		}
		page.setQuery(sb.toString());
		page.setQueryCount("select count(t.id) " + sb);
//		page.setQuery(sb.insert(" from MenuPackage t".length(), " join fetch t.product ").toString());
		this.menuPackageDAO.queryByPage(page, paras.toArray());
		DTOPagedList<DTOMenuPackage> pageList = new DTOPagedList<DTOMenuPackage>();
		pageList.setPageNo(page.getPageNo());
		pageList.setPageSize(page.getPageSize());
		pageList.setTotalCount(page.getTotalCount());
		List<DTOMenuPackage> ls = new ArrayList<DTOMenuPackage>();
		for (MenuPackage or : page.getList()) {
			DTOMenuPackage dto = new DTOMenuPackage();
			dto = MenuPackageMapper.getInstance().mapToValue(or);
			
			ls.add(dto);
		}
		pageList.setList(ls);
		return pageList;
	}

	public void disable(long id) {
		// TODO Auto-generated method stub
		MenuPackage vo = this.menuPackageDAO.find(id);
		vo.setStatus(PackageStatus.HIDDEN);
		this.menuPackageDAO.merge(vo);
	}
	public MenuPackage getPackgeByName(String name) {
		
		return this.menuPackageDAO.getPackgeByName(name);
	}
}
